import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/module/world/world_content_publish/world_content_publish_page.dart';

import '../../../common/keep_alive_wrapper.dart';
import '../../../theme/font.dart';
import 'world_list_page.dart';

/// @description:
/// @author
/// @date: 2023-11-24 15:12:22
class WorldMainPage extends StatefulWidget {
  const WorldMainPage({Key? key}) : super(key: key);

  @override
  State<WorldMainPage> createState() => _WorldMainPageState();
}

class _WorldMainPageState extends State<WorldMainPage> with TickerProviderStateMixin {
  late TabController _tabController;
  final List<Widget> _pages = [];

  late String category;

  final List<String> _tabs = ['关注', '世界', '附近'];

  late final PageController _controller = PageController(initialPage: 0);
  final CustomTabBarController _tabBarController = CustomTabBarController();

  @override
  void initState() {
    super.initState();
    _pages.add(const WorldListPage(categoryId: 0, key: PageStorageKey('关注')));
    _pages.add(const WorldListPage(categoryId: 1, key: PageStorageKey('世界')));
    _pages.add(const WorldListPage(categoryId: -1, key: PageStorageKey('附近')));
    _tabController = TabController(length: _pages.length, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KeepAliveWrapper(
      child: Stack(
        children: [
          SizedBox(
              width: double.infinity,
              child: Image.asset('assets/app/images/world/world_top_bg.png', fit: BoxFit.cover)),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
                title: _buildTopBarWrapper(),
                backgroundColor: Colors.transparent,
                elevation: 0,
                systemOverlayStyle: SystemUiOverlayStyle.dark),
            body: PageView.builder(
                physics: const ClampingScrollPhysics(),
                controller: _controller,
                itemCount: _tabs.length,
                itemBuilder: (context, index) {
                  return _pages[index];
                }),
            floatingActionButton: GestureDetector(
              onTap: () {
                Get.to(const WorldContentPublishPage());
              },
              child: Image.asset('assets/app/images/world/world_publsh.png', width: 54.w),
            ),
          )
        ],
      ),
    );
  }

  Container _buildTopBarWrapper() {
    return Container(
      margin: const EdgeInsets.only(bottom: 0),
      child: Row(
        children: [
          Expanded(
            child: CustomTabBar(
              tabBarController: _tabBarController,
              height: 35,
              width: 140,
              // pinned: true,
              itemCount: _tabs.length,
              builder: (BuildContext context, int index) {
                return TabBarItem(
                    transform: ScaleTransform(
                        maxScale: 1.1,
                        transform: ColorsTransform(
                            highlightColor: const Color(0xFF263B38),
                            normalColor: const Color(0xFF263B38).withOpacity(0.8),
                            builder: (context, color) {
                              return Container(
                                  padding: const EdgeInsets.only(bottom: 5),
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints(minWidth: 60),
                                  child: (Text(_tabs[index],
                                      style: TextStyle(fontSize: 18, color: color, fontWeight: AppFontWeight.medium))));
                            })),
                    index: index);
              },
              indicator: RoundIndicator(color: Colors.transparent, line: true),
              pageController: _controller,
            ),
          ),
        ],
      ),
    );
  }
}
